package com.xiaoc.bulldozer.facade.impl;

import com.xc.framework.common.base.ResponseCode;
import com.xc.framework.common.exception.BusinessException;
import com.xc.framework.common.utils.NumberUtil;
import com.xiaoc.bulldozer.common.AccountBalanceConstant;
import com.xiaoc.bulldozer.common.Constant;
import com.xiaoc.bulldozer.common.dto.AccountDto;
import com.xiaoc.bulldozer.common.dto.AccountQueryDto;
import com.xiaoc.bulldozer.common.model.Account;
import com.xiaoc.bulldozer.common.model.AccountBalance;
import com.xiaoc.bulldozer.common.model.WeixinUser;
import com.xiaoc.bulldozer.common.service.AccountBalanceService;
import com.xiaoc.bulldozer.common.service.AccountService;
import com.xiaoc.bulldozer.facade.AccountFacade;
import com.xiaoc.bulldozer.facade.WeixinUserFacade;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * describe:
 *
 * @author lihuancheng
 * @date 2018/9/12
 */
@Service("accountFacade")
public class AccountFacadeImpl implements AccountFacade {

    @Resource(name = "accountService")
    private AccountService accountService;

    @Resource(name = "weixinUserFacade")
    private WeixinUserFacade weixinUserFacade;

    @Resource(name = "accountBalanceService")
    private AccountBalanceService accountBalanceService;


    @Override
    public List<AccountDto> getAccountList(AccountQueryDto accountQueryDto) {

        List<AccountDto> accountDtoList = accountService.getAccountList(accountQueryDto);
        for (AccountDto accountDto : accountDtoList) {
            if (NumberUtil.equals(accountDto.getType(), Constant.ACCOUNT_TYPE.USER)) {
                accountDto.setTypeName("注册用户");
            } else if (NumberUtil.equals(accountDto.getType(), Constant.ACCOUNT_TYPE.MANAGER)){
                accountDto.setTypeName("渠道经理");
            } else if (NumberUtil.equals(accountDto.getType(), Constant.ACCOUNT_TYPE.SHARER_TELENT)) {
                accountDto.setTypeName("分享达人");
            } else {
                accountDto.setTypeName("注册用户");
            }
        }

        return accountDtoList;
    }

    @Override
    public Integer getAccountCount(AccountQueryDto accountQueryDto) {
        return accountService.getAccountCount(accountQueryDto);
    }

    @Override
    public void updateAccount(AccountDto accountDto) {
        Account account = accountService.getAccountById(accountDto.getId());
        if (account == null) {
            throw new BusinessException(ResponseCode.INTERNAL_ERROR, "用户信息不存在");
        }

        if (NumberUtil.equals(account.getType(), Constant.ACCOUNT_TYPE.MANAGER)
                && (NumberUtil.equals(accountDto.getType(), Constant.ACCOUNT_TYPE.USER) || NumberUtil.equals(accountDto.getType(), Constant.ACCOUNT_TYPE.SHARER_TELENT))) {

            throw new BusinessException(ResponseCode.INTERNAL_ERROR, "渠道经理不能改为注册用户或分享达人");
        }

        boolean isChangeType = true;
        if (NumberUtil.equals(account.getType(), accountDto.getType())) {
            isChangeType = false;
        }

        long currentTime = System.currentTimeMillis();
        account.setUpdateTime(currentTime);
        account.setType(accountDto.getType());
        accountService.update(account);

        if (isChangeType) {

            WeixinUser weixinUser = new WeixinUser();
            weixinUser.setId(account.getUserId());

            if (NumberUtil.equals(accountDto.getType(), Constant.ACCOUNT_TYPE.USER)) {
                weixinUser.setUserType(WeixinUser.TYPE_REGISTER);
            } else if (NumberUtil.equals(accountDto.getType(), Constant.ACCOUNT_TYPE.MANAGER)) {
                weixinUser.setUserType(WeixinUser.TYPE_MANAGER);
            } else if (NumberUtil.equals(accountDto.getType(), Constant.ACCOUNT_TYPE.SHARER_TELENT)) {
                weixinUser.setUserType(WeixinUser.TYPE_SHARE_TALENT);
            }

            weixinUserFacade.updateWeixinUser(weixinUser);

            //初始化渠道经理账户
            if (NumberUtil.equals(account.getType(), Constant.ACCOUNT_TYPE.MANAGER) || NumberUtil.equals(account.getType(), Constant.ACCOUNT_TYPE.SHARER_TELENT)) {
                AccountBalance accountBalance = accountBalanceService.getAccountBalanceAccountIdAndType(account.getId(), AccountBalanceConstant.ACCOUNT_BALANCE_TYPE_CASH);
                if (accountBalance == null) {
                    synchronized (this) {
                        accountBalance = accountBalanceService.getAccountBalanceAccountIdAndType(account.getId(), AccountBalanceConstant.ACCOUNT_BALANCE_TYPE_CASH);
                        if (accountBalance == null) {
                            accountBalance = new AccountBalance();
                            accountBalance.setUserId(account.getUserId());
                            accountBalance.setAccountId(account.getId());
                            accountBalance.setBalance(0);
                            accountBalance.setType(AccountBalanceConstant.ACCOUNT_BALANCE_TYPE_CASH);
                            accountBalance.setUpdateTime(currentTime);
                            accountBalance.setCreateTime(currentTime);

                            accountBalanceService.save(accountBalance);
                        }
                    }
                }
            }
        }
    }


    @Override
    public Account getAccountById(Integer id) {

        return accountService.getAccountById(id);
    }

    @Override
    public List<AccountDto> getAccountForTotal(AccountQueryDto queryDto) {
        return null;
    }

}
